home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 12 / Amiga Format AFCD12 (Apr 1997, Issue 96).iso / -in_the_mag- / html_tutorial / mas_try9.cpp < prev    next >
C/C++ Source or Header  |  1997-01-21  |  3KB  |  150 lines

  1. // (C) M.A.Smith University of Brighton
  2. //
  3. // Permission is granted to use this code
  4. //   provided this declaration and copyright notice remains intact.
  5. //
  6. // 21 October 1995
  7.  
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <iomanip.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14.  
  15. #define NO_MAP
  16.  
  17. #define SERVER  "http://snowwhite.it.brighton.ac.uk/"
  18. #define TRY_PROG "cgi-bin/mas/mas_try2"
  19.  
  20. #include "parse.h"
  21. #include "parse.cpp"
  22.  
  23. void process_script( char [], char [] );
  24. void process( char [] );
  25. char* getenv_n( char [] );
  26.  
  27. inline void html( char str[] )
  28. {
  29.   cout << str << "\n";
  30. }
  31.  
  32. inline void html_( char str[] )
  33. {
  34.   cout << str;
  35. }
  36.  
  37. inline void html_( char c )
  38. {
  39.   cout << c;
  40. }
  41.  
  42. // Main program
  43. //
  44.  
  45. int main()
  46. {
  47.   char *query_str = getenv("QUERY_STRING");
  48.   Parse list( query_str == 0 ? "file=html.htm&seg=H6" : query_str );
  49.  
  50.   process_script( list.get_item( "file", 1, true ), 
  51.                   list.get_item( "seg" ) );
  52.   return 0;
  53. }
  54.  
  55.  
  56. void process_script( char file[], char seg[] )
  57. {
  58.   enum State { NORMAL, IN_ESC, SAVE };
  59.   html( "Content-type: text/html" );
  60.   html( "" ); html( "" );
  61.   ifstream script( file );
  62.   if ( script.fail() ) return;
  63.   script >> resetiosflags( ios::skipws );
  64.   const int MAX_MES = 200;
  65.   char  c;
  66.   char  mes[ MAX_MES ];
  67.   char  start[] = "<TRY-ME";  const int START_LEN = strlen( start );
  68.   char  end[]   = "</TRY-ME>"; const int END_LEN = strlen( start );
  69.   int   i, p=0, mes_len = 0;
  70.   State state_is = NORMAL;
  71.  
  72.   while ( script >> c, !script.eof() )
  73.   {
  74.     switch ( state_is )
  75.     {
  76.       case NORMAL :
  77.     if ( c == start[p] ) {         // Escape Sequence
  78.       p++;
  79.       if ( p >= START_LEN ) {      // Recognized Escape Seq.
  80.         state_is = IN_ESC;
  81.         mes_len = 0; p = 0;
  82.       }
  83.     } else {                       // Not Escape Seq.
  84.       p = 0;
  85.     }
  86.     break;
  87.       case IN_ESC :                    // In Escape Sequence
  88.         if ( c != '>' )
  89.         {
  90.           mes[p] = c;
  91.           p++;
  92.         } else {
  93.           mes[p] = '\0';
  94.           Parse rest(mes);
  95.       if ( strcmp( rest.get_item_n( "name" ), seg ) ) {
  96.             state_is = SAVE; p = 0;
  97.           }
  98.         }
  99.         break;
  100.       case SAVE :
  101.     if ( c == end[p] ) {           // End of Escape Seq
  102.       p++;
  103.       if ( p >= END_LEN ) {        // End of Escape Seq
  104.         mes[ mes_len ] = '\0';     // Message to process
  105.         process( mes );
  106.         p = 0; state_is = NORMAL;
  107.       }
  108.     } else {                       // Process gatherd chrs
  109.       if ( mes_len < MAX_MES - 10 ) {
  110.         for ( i = 0; i< p; i++ ) {
  111.           mes[mes_len++] = end[i];
  112.         }
  113.         mes[mes_len++] = c; p = 0; // Add to gatherd chrs
  114.       }
  115.     }
  116.     break;
  117.     }
  118.   }
  119. }
  120.     
  121. void process( char mes[] )
  122. {
  123.   html("<HTML>");
  124.  
  125.   html("<HEAD>");
  126.   html("<TITLE>Example of HTML</TITLE>");
  127.   html("</HEAD>");
  128.  
  129.   html("<BODY>");
  130.  
  131.   html("<P>Try out the HTML<P> ");
  132.   html("<FORM ACTION=\"" SERVER TRY_PROG "\"" );
  133.   html("<TEXTAREA NAME=\"feedback\" ROWS=10 COLS=60>" );
  134.   html( mes );
  135.   html("</TEXTAREA>");
  136.   html("<INPUT TYPE=\"submit\" NAME=\"action\" VALUE=\"Execute HTML\">" );
  137.   html("</FORM>");
  138.  
  139.   html("</BODY>");
  140.   html("</HTML>");
  141. }
  142.  
  143.  
  144. char* getenv_n( char var[] )
  145. {
  146.   char *p = getenv( var );
  147.   return p == NULL ? "[]" : p;
  148. }
  149.  
  150.